package com.tinckay.service.impl;

import com.tinckay.common.constant.ProjectConstant;

import com.tinckay.common.constant.RedisConstant;
import com.tinckay.common.exception.ServiceException;

import com.tinckay.common.util.LocalDateTimeUtils;
import com.tinckay.common.util.ShiroUtils;
import com.tinckay.entity.po.Task;
import com.tinckay.entity.po.TranProcess;
import com.tinckay.entity.po.User;
import com.tinckay.mapper.TranProcessMapper;
import com.tinckay.service.TranProcessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinckay.service.VehicleService;
import net.bytebuddy.asm.Advice;
import cn.hutool.core.util.StrUtil;
import org.aspectj.weaver.AnnotationTargetKind;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jean
 * @since 2018-10-24
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TranProcessServiceImpl extends ServiceImpl<TranProcessMapper, TranProcess> implements TranProcessService {

    @Autowired
    private TaskServiceImpl taskService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTranProcess(TranProcess tranProcess) {

        int processType = tranProcess.getProcessType();
        int charge = ProjectConstant.TASK_TYPE.CHARGE.getCode();
        int transport = ProjectConstant.TASK_TYPE.TRANSPORT.getCode();
        int discharge = ProjectConstant.TASK_TYPE.DISCHARGE.getCode();
        int dischargeSuspension = ProjectConstant.TASK_TYPE.DISCHARGE_SUSPENSION.getCode();
        int returnCode = ProjectConstant.TASK_TYPE.RETURN.getCode();

        Task task = getTaskInfo(tranProcess.getTaskId());

        switch (processType) {
            case 1:
                if (0 != task.getTaskStatus()) {
                    throw new ServiceException("运输过程状态有误");
                }
                task.setStartTime(tranProcess.getStartTime());
                updateTaskInfo(task, charge);
                break;
            case 2:
                if (charge != task.getTaskStatus()) {
                    throw new ServiceException("操作有误,装料未结束或已完成运输");
                }
                updateTaskInfo(task, transport);
                break;
            case 3:
                if (transport != task.getTaskStatus() && dischargeSuspension != task.getTaskStatus()) {
                    throw new ServiceException("操作有误,运输未结束或已完成卸料");
                }
                //获取仓体编号
                //String barnNo = (String) redisTemplate.opsForValue().get(RedisConstant.ACTIVE_STOREHOUSE_CODE);

                //if (StrUtil.isBlank(barnNo)) {
                //    throw new ServiceException("暂无可用活动仓体编号");
                //}
                //task.setBarnNo(barnNo);
                updateTaskInfo(task, discharge);
                break;
            case 4:
                if (transport != task.getTaskStatus() && dischargeSuspension != task.getTaskStatus()) {
                    throw new ServiceException("操作有误,运输未结束");
                }
                updateTaskInfo(task, dischargeSuspension);
                break;
            case 5:
                if (discharge != task.getTaskStatus()) {
                    throw new ServiceException("操作有误,卸料未结束");
                }
                task.setEndTime(tranProcess.getEndTime());
                task.setConsumeTime(LocalDateTimeUtils.betweenSeconds(task.getStartTime(), task.getEndTime()));
                updateTaskInfo(task, returnCode);
                break;
            default:
                throw new ServiceException("运输过程状态有误");
        }

        tranProcess.setConsumeTime(LocalDateTimeUtils.betweenSeconds(tranProcess.getStartTime(), tranProcess.getEndTime()));
        this.baseMapper.insert(tranProcess);
    }

    private void updateTaskInfo(Task task, int taskStatus) {
        task.setTaskStatus(taskStatus);
        task.updateById();
    }

    private Task getTaskInfo(Long taskId) {
        if (null == taskId) {
            throw new ServiceException("任务id不能为空");
        }
        Task task = taskService.getById(taskId);
        if (null == task) {
            throw new ServiceException("查无该运输任务信息");
        }
        return task;
    }

}
